<?php
namespace app\admin\controller;
use app\model\BaiduVideo as BaiduVideoModel;
use think\Db;


class BaiduVideo extends Base
{
    public function gets(){
        $api_url = 'https://cloud.baidu.com/api/partner-video/list';
        $params = '{"pageNo":1,"pageSize":100,"type":"all","domain":"百度智能云千帆AI加速器","product":"all","third":"","search":"","orderBy":"default"}';

        $list = httpRequest($api_url,$params,'POST',[
            'Content-Type: application/json'
        ]);
        $list = json_decode($list,true);
        Db::name('baidu_video')->insertAll($list['page']['result']);
        dump($list);
    }

    public function getVideo(){
        ini_set('max_execution_time', 0);
        set_time_limit(0);

        $list = Db::name('baidu_video')->where(Db::raw('url is null'))->select();
        foreach ($list as $k => $vo) {
            $src = urldecode($vo['src']);
            $ext = explode(".", $vo['src'])[count(explode(".", $vo['src']))-1];
            $video_content = file_get_contents($src);
            if(empty($video_content)){
                continue;
            }
            file_put_contents('./baidu_video/'.$vo['name'].'.'.$ext,$video_content);
            $result = uploadFile($vo['name'].'.'.$ext,'./baidu_video/'.$vo['name'].'.'.$ext,'baidu_video/',true);
            if($result['status'] == 1){
                Db::name('baidu_video')->where('id',$vo['id'])->update(['url'=>$result['data']]);
            }
        }
        echo 'success';
    }

    public function __construct(){
        $model = new BaiduVideoModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'image'=>['name'=>'封面，有视频可不传','type'=>'file'],
            'product'=>['name'=>'产品','type'=>'text'],
            'videoDesc'=>['name'=>'视频描述','type'=>'text'],
            'url'=>['name'=>'视频链接，填视频文件名','type'=>'text'],
            'desc'=>['name'=>'描述','type'=>'textarea'],
            'content'=>['name'=>'文档','type'=>'ueditor'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
      
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['image','url','product','videoDesc','desc','content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>['name'],
                'nomust'=>['image','url','product','videoDesc','desc','content']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }

    public function detail(){
        $model = $this->model;
        $ids = input('ids');
        $list = $model->where('id','in',$ids)->select();
        foreach ($list as $k => $vo) {
            
        }
        return json(['code'=>2000,'data'=>$list]);
    }
    


   
}
